﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using TradingAppDal;
using TradingApp.Helper;

namespace TradingApp.Admin
{
    public partial class frmAdminSettings : Form
    {
        public frmAdminSettings()
        {
            InitializeComponent();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                string sValidationError = ValidateUI();
                if (String.IsNullOrEmpty(sValidationError))
                {
                    TradingAppSettings.DBConnectionStr = tbConnStr.Text.Trim();
                    TradingAppSettings.MaxThreadCount = Convert.ToInt32(tbThreadCount.Text.Trim());
                    TradingAppSettings.PoolIntervalInSecs = Convert.ToInt32(tbPoolInterval.Text.Trim());
                    TradingAppSettings.DefaultReport = cboReport.Text;
                    XmlHelper.SetTagValue(TradingAppSettings.CONN_ATTRIBUTE_STR, TradingAppSettings.DBConnectionStr);
                    XmlHelper.SetTagValue(TradingAppSettings.MAXTHREAD_ATTRIBUTE_STR, TradingAppSettings.MaxThreadCount.ToString());
                    XmlHelper.SetTagValue(TradingAppSettings.TIMEOUT_ATTRIBUTE_STR, TradingAppSettings.PoolIntervalInSecs.ToString());
                    XmlHelper.SetTagValue(TradingAppSettings.DEFAULTREPORT_ATTRIBUTE_STR, TradingAppSettings.DefaultReport);
                    MessageBox.Show("Settings changed successfully.", "Settings", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {
                    MessageBox.Show(sValidationError, "Settings", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            catch (System.Exception ex)
            {
                UIHelper.HandleApplicationException(ex);
            }
        }

        private string ValidateUI()
        {
            StringBuilder sb = new StringBuilder();
            if (String.IsNullOrEmpty(tbConnStr.Text.Trim()))
            {
                sb.Append("Connection String cannot be empty.");
            }
            if (!AdminDal.TestConnectionString(tbConnStr.Text.Trim()))
            {
                sb.Append(Environment.NewLine + "Database could not be connected using connection string.");
            }
            CheckInt(sb, tbPoolInterval.Text.Trim(), "Pool interval", 10000);
            CheckInt(sb, tbThreadCount.Text.Trim(), "Thread count", 100);

            return sb.ToString();
        }

        private void CheckInt(StringBuilder sb, string tbValue, string tbUIValue, int maxValue)
        {
            if (String.IsNullOrEmpty(tbValue))
            {
                sb.Append(Environment.NewLine + tbUIValue + " cannot be empty.");
            }
            int temp = 0;
            if (!int.TryParse(tbValue, out temp))
            {
                sb.Append(Environment.NewLine + tbUIValue + " is not a valid integer.");
            }
            if (temp < 1)
            {
                sb.Append(Environment.NewLine + tbUIValue + " cannot be zero.");
            }
            if (temp > maxValue)
            {
                sb.Append(Environment.NewLine + tbUIValue + " cannot be greater than " + maxValue + " .");
            }
        }
        private void frmPoolInterval_Load(object sender, EventArgs e)
        {
            tbConnStr.Text = TradingAppSettings.DBConnectionStr;
            tbThreadCount.Text = TradingAppSettings.MaxThreadCount.ToString();
            tbPoolInterval.Text = TradingAppSettings.PoolIntervalInSecs.ToString();
            if (AdminDal.TestConnectionString(tbConnStr.Text.Trim()))
            {
                UIHelper.BindReports(cboReport);
            }
        }

        
        
        private void btnTestConnection_Click(object sender, EventArgs e)
        {
            try
            {
                if (AdminDal.TestConnectionString(tbConnStr.Text.Trim()))
                {
                    MessageBox.Show("Connected.", "Connection String", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {
                    MessageBox.Show("Can't Connect.", "Connection String", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            catch (System.Exception ex)
            {
                UIHelper.HandleApplicationException(ex);
            }
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }


    }
}
